<?php

/**
 * ZView
 *
 * Zend Frameworks View Component for TYPO3
 *
 * @package    ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
*/


/** Abstract class for extension */
require_once 'Zend/View/Helper/Abstract.php';

/**
 * Helper to work with TYPO3 RTE text
 *
 * @package ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
 */
class Tx_Zview_Helper_T3rte extends Zend_View_Helper_Abstract
{

	private static $_rteInstanceCounter;

	/**
	 * The TYPO3 plugin
	 *
	 * @var tslib_pibase
	*/
	private $_pi;

	/**
	 * Transforms content saved by the RTE. This is the shortform
	 * of t3rte()->transform().
	 *
	 * If no arguments are passed, returns a Tx_Zview_Helper_T3rte object.
	 *
	 * @param string $input
	 * @return string|Tx_Zview_Helper_T3rte
	 */
	public function t3rte($input = null)
	{
        if (0 == func_num_args()) {
            return $this;
        }
		return $this->transform($input);
	}

	/**
	 * Transforms content saved by the RTE into suitable html formats.
	 * Eg. transforms TYPO3-typolinks (<link></link>) into HTML <a> anchors
	 *
	 * @param string $input
	 * @return string
	*/

	public function transform($input) {
		return $this->_pi->pi_RTEcssText($input);
	}

	/**
	 * Enter description here...
	 *
	 * @param string $field
	 * @param string $value
	 * @return unknown
	 */
	public function editor($field, $value)
	{

		require_once(t3lib_extMgm::extPath('rtehtmlarea').'pi2/class.tx_rtehtmlarea_pi2.php');

		self::$_rteInstanceCounter++;

		$this->_pi->RTEObj;
		$this->_pi->docLarge = 0;
		$this->_pi->RTEcounter = self::$_rteInstanceCounter;
		$this->_pi->formName = '';
		$this->_pi->additionalJS_initial = '';
		$this->_pi->additionalJS_pre = array();
		$this->_pi->additionalJS_post = array();
		$this->_pi->additionalJS_submit = array();
		$this->_pi->PA = array(
			'itemFormElName' =>  '',
			'itemFormElValue' => '',
		);
		$this->_pi->specConf = array(
			'rte_transform' => array(
				'parameters' => array('mode' => 'ts_css')
			)
		);
		$this->_pi->thisConfig = array();
		$this->_pi->RTEtypeVal = 'text';
		$this->_pi->thePidValue = $GLOBALS['TSFE']->id;

		if(!$this->_pi->RTEObj) $this->_pi->RTEObj = t3lib_div::makeInstance('tx_rtehtmlarea_pi2');
		if($this->_pi->RTEObj->isAvailable()) {
			$this->_pi->PA['itemFormElName'] = $this->_pi->prefixId . '[' . $field . ']';
			$this->_pi->PA['itemFormElValue'] = $value;
			$this->_pi->thePidValue = $GLOBALS['TSFE']->id;
			$RTEItem = $this->_pi->RTEObj->drawRTE($this->_pi, '', '', $row=array(), $this->_pi->PA, $this->_pi->specConf, $this->_pi->thisConfig, $this->_pi->RTEtypeVal, '', $this->_pi->thePidValue);

			$markerArray['ADDITIONALJS_PRE'] = $this->_pi->additionalJS_initial.'
			<script type="text/javascript">'. implode(chr(10), $this->_pi->additionalJS_pre).'
			</script>';
			$markerArray['ADDITIONALJS_POST'] = '
			<script type="text/javascript">'. implode(chr(10), $this->_pi->additionalJS_post).'
			</script>';
			$markerArray['ADDITIONALJS_SUBMIT'] = implode(';', $this->_pi->additionalJS_submit);
			$markerArray['FORM_RTE_ENTRY'] = $RTEItem;
		}

		$onsubmit = "
			<script type=\"text/javascript\">
				/*<![CDATA[*/

				function rteHandler" . $this->_pi->RTEcounter . "()
				{
					" . $markerArray['ADDITIONALJS_SUBMIT'] . "
				}

				/*]]>*/
			</script>";

		return $onsubmit
		 . $markerArray['ADDITIONALJS_PRE']
		 . $markerArray['FORM_RTE_ENTRY']
		 . $markerArray['ADDITIONALJS_POST'];

	}

	/**
	 * Set the View object
	 *
	 * @param  Zend_View_Interface $view
	 * @return Zend_View_Helper_Abstract
	*/
	public function setView(Zend_View_Interface $view) {
		$this->view = $view;
		$this->_pi = $view->getPi();
		if(empty($this->_pi) || (!$this->_pi instanceof tslib_pibase)) {
			throw new Exception('Please set tslib_pibase using $view->setPi($this) in your plugin class.');
		}
		return $this;
	}

	private function _testPiMemberVars()
	{
		/*
		var $RTEObj;
	    var $strEntryField;
	    var $docLarge = 0;
	    var $RTEcounter = 0;
	    var $formName;
	    var $additionalJS_initial = '';		// Initial JavaScript to be printed before the form (should be in head, but cannot due to IE6 timing bug)
		var $additionalJS_pre = array();	// Additional JavaScript to be printed before the form
		var $additionalJS_post = array();	// Additional JavaScript to be printed after the form
		var $additionalJS_submit = array();	// Additional JavaScript to be executed on submit
	    var $PA = array(
	            'itemFormElName' =  '',
	            'itemFormElValue' = '',
	            );
	    var $specConf = array();
	    var $thisConfig = array();
	    var $RTEtypeVal = 'text';
	    */
	}

}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3rte.php']) {
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3rte.php']);
}
